<?php
/**
 * Author: yixuan
 * Desc:
 * Time: 2016/7/8 09:46
 */

namespace app\mobile\controller;


use app\lib\WxoffiAccount;
use think\Controller;
use think\Cookie;
use think\Session;

class Auth extends Controller
{
    /**
     * Desc:授权页逻辑
     * Author: yixuan
     * Time: 2016/7/8 10:05
     * @return null
     */
    public function auth()
    {
        global $_P, $_G;
        $code = isset($_P['code']) ? $_P['code'] : '';
        $state = isset($_P['state']) ? $_P['state'] : '';
        //通过code获取网页授权access_token
        $access_token_info = WxoffiAccount::getInstance()->getUserAccessToken($code);
        //member表信息检测
        $member_info = $this->checkMemberInfo($access_token_info, $state);
        if ($member_info && $member_info['openid']) {
            Session::set('openid', $member_info['openid']);
        }
        //判断session中是否存在跳转前页面
        if (Session::has('auth_pre_page')) {
            $tmp_auth_pre_page = Session::get('auth_pre_page');
            //替换 NewUserLogin 为 index
            $start = strrpos($tmp_auth_pre_page, '/');
            $end = strpos($tmp_auth_pre_page, '.aspx');
            if ($start && $end) {
                $do = substr($tmp_auth_pre_page, $start, $end - $start);
                $do = strtolower(substr($do, 1));
                if (!in_array($do, ['index', 'x_input', 'w_input', 'w_init', 'x_init', 'newloginuser'])) {
                    yxlog($do . $tmp_auth_pre_page, '授权页2');
                    $tmp_auth_pre_page = str_replace($do, 'index', $tmp_auth_pre_page);
                }
            }
            return $this->redirect($tmp_auth_pre_page);
        } else {
            //不存在默认跳转首页
            return $this->redirect(createMobileUrl('index', '', true));
        }
    }

    /**
     * Desc:判断用户信息 挂载全局变量
     * Author: yixuan
     * Time: 2016/7/8 10:06
     * @param $user_access_token
     * @param $state
     * @return array['is_auth'=>0,1 0未授权头像 1已授权头像昵称]
     */
    protected function checkMemberInfo($user_access_token, $state)
    {
        global $_G;
        //挂载全局openid
        if ($user_access_token['openid']) {
            $_G['openid'] = $user_access_token['openid'];
        }
        //判断state是否为YXSTATEPOP
        if ($state == 'YXSTATEPOP') {
            //拉取用户信息
            $user_info = WxoffiAccount::getInstance()->getUserInfo($user_access_token['access_token'], $user_access_token['openid']);
            if (!isset($user_info['errcode']) && $user_info['headimgurl'] && $user_info['nickname']) {
                //更新member表信息
                $user_avatar = $user_info['headimgurl'];
                $user_nickname = $user_info['nickname'];
            }
        }
        //todo 查询member表是否存在记录
        $member_res = member_info($user_access_token['openid']);
        if (empty($member_res)) {
            $user_avatar = 'http://css2020.yixuan.net/2020css/yx2020/images/question.png';
            $user_nickname = '微信用户';
            $insert_data = [
                'mpid' => $_G['mpid'],
                'openid' => $user_access_token['openid'],
                'unionid' => '',
                'nickname' => $user_nickname,
                'avatar' => $user_avatar,
                'realname' => '',
                'mobile' => '',
                'updatetime' => time()
            ];
            $insert_res = member_save($insert_data);
            $insert_data['uid'] = $insert_res['uid'];
            $insert_data['is_auth'] = 0;
            unset($insert_data['unionid']);
            //挂载全局member
            $_G['member'] = $insert_data;
            //挂载全局uid
            $_G['uid'] = $insert_data['uid'];
            return $insert_data;
        }
        if ($state == 'YXSTATEPOP') {
            if ($user_avatar != 'http://css2020.yixuan.net/2020css/yx2020/images/question.png' && $user_nickname != '微信用户') {
                //更新member表中的头像昵称
                member_save(['nickname' => stripcslashes(filterEmoji($user_nickname)), 'avatar' => $user_avatar, 'updatetime' => time()], $user_access_token['openid']);
                $member_res['nickname'] = stripcslashes(filterEmoji($user_nickname));
                $member_res['avatar'] = $user_avatar;
            }
        }
        unset($member_res['unionid']);
        $member_res['is_auth'] = 0;
        if ($member_res['nickname'] && $member_res['avatar'] && $member_res['nickname'] != '微信用户' && $member_res['avatar'] != 'http://css2020.yixuan.net/2020css/yx2020/images/question.png') {
            $member_res['is_auth'] = 1;
        }
        unset($member_res['unionid']);
        //挂载全局member
        $_G['member'] = $member_res;
        //挂载全局uid
        $_G['uid'] = $member_res['uid'];
        return $member_res;
    }
}